import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created by LiuChang on 2017/4/18.
 */
public class BaiDuApi {
    public static final String AK = "KbZ1lbG8msBdM0fImY1nXdTsGjx3vGgL";

    public static void main(String[] args) throws IOException {
        RequestConfig requestConfig = RequestConfig.DEFAULT;
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();


      /*  HttpPost httpPost = new HttpPost("http://api.map.baidu.com/place/v2/search");
        httpPost.setHeader("Content-Type", "application/json");

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("ak", AK));
        params.add(new BasicNameValuePair("query", "银泰城"));
        params.add(new BasicNameValuePair("tag", "干锅"));
        params.add(new BasicNameValuePair("output", "json"));
        params.add(new BasicNameValuePair("scope", "1"));
        params.add(new BasicNameValuePair("region", "成都"));
        params.add(new BasicNameValuePair("page_size", "10"));
        params.add(new BasicNameValuePair("page_num", "0"));
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, Consts.UTF_8);
        httpPost.setEntity(urlEncodedFormEntity);
        System.out.println(EntityUtils.toString(urlEncodedFormEntity));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        System.out.println(httpResponse.getStatusLine().getStatusCode());
        System.out.println(EntityUtils.toString(httpResponse.getEntity()));*/

        //只能用get方式，不能用post方式。否则报错302
        String params = "?ak=KbZ1lbG8msBdM0fImY1nXdTsGjx3vGgL&" +
                "query=" + URLEncoder.encode("银泰城", "UTF-8") +
                "&tag=" + URLEncoder.encode("干锅", "UTF-8") +
                "&region=" + URLEncoder.encode("成都", "UTF-8") +
                "&page_size=10&page_num=0" +
                "&output=json&scope=1";

        HttpGet httpGet = new HttpGet("http://api.map.baidu.com/place/v2/search" + params);

        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        System.out.println(EntityUtils.toString(httpResponse.getEntity()));

    }
}
